<?php

define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

// set timeout session
ini_set('session.gc-maxlifetime', 60*30);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';
JDEBUG ? $_PROFILER->mark('afterLoad') : null;
$app = JFactory::getApplication('administrator');

// load librarie B&G
include JPATH_BASE.'/includes/bgHelper.php';

// Initialise the application.
$app->initialise(array(
    'language' => $app->getUserState('application.lang')
));
/****** end thư viện joomla **/

/******************************************************************/
    $db = JFactory::getDbo();   

    /* */
    $query = "SELECT * FROM `mama` WHERE `MAMA_DATETIME` <= NOW() AND `MAMA_TYPE` in ( 1,3 ) AND `MAMA_FLAG` = 2";
    $db -> setQuery($query); 
    $result = $db -> loadObjectList();
    print_r($result);
    foreach ($result as $var)
    {       
        if ($var->MAMA_TYPE == 1)
        {print_r($var->MAMA_MAGR_CODE);
            HelperBG::sendMail($var->MAMA_MAGR_CODE,$var->MAMA_TITLE,$var->MAMA_BODY,null, null,$var->MAMA_REJECT);
            $update = "UPDATE `mama` SET MAMA_FLAG = '3' WHERE `MAMA_CODE` = '".$var->MAMA_CODE."'";
            $db -> setQuery($update);
            $db -> query();
        }
        else 
        {
            /* get date next month */
            if(date('m',mktime(0, 0, 0, date("m")+2, date("d"), date("Y"))) %12 == 1)
            {
                $date=date('Y-m-d H:i:s',mktime(0, 0, 0, date("m")-11, 01, date("Y")+1)); 
            }else {
                $date=date('Y-m-d H:i:s',mktime(0, 0, 0, date("m")+2, 01, date("Y")));
            }

            /* get date next month */
            if(date('m',mktime(0, 0, 0, date("m")+1, date("d"), date("Y"))) %12 == 1)
            {
                $date_1=date('Y-m-d H:i:s',mktime(0, 0, 0, date("m")-11, 01, date("Y")+1)); 
            }else {
                $date_1=date('Y-m-d H:i:s',mktime(0, 0, 0, date("m")+1, 01, date("Y")));
            }
                
            $query = "SELECT COCH_NAME,COCH_MAIL,COCH_MAIW,COCH_MAIK 
                        FROM coch 
                        WHERE COCH_LDATE >= '".$date_1."' 
                        AND COCH_LDATE < '".$date."' 
                        AND (COCH_MAIF = 1 OR COCH_MAFW = 1 OR COCH_MAKF = 1)";       
            $db -> setQuery($query); 
            $coch = $db -> loadObjectList();
            HelperBG::sendMail($coch,$var->MAMA_TITLE,$var->MAMA_BODY,'COCH_MAIL','COCH_NAME',1);
            HelperBG::sendMail($coch,$var->MAMA_TITLE,$var->MAMA_BODY,'COCH_MAIW','COCH_NAME',1);
            HelperBG::sendMail($coch,$var->MAMA_TITLE,$var->MAMA_BODY,'COCH_MAIK','COCH_NAME',1);

            /* get date next month */
            if(date('m',mktime(0, 0, 0, date("m")+1, date("d"), date("Y"))) %12 == 1)
            {
                $date=date('Y-m-d H:i:s',mktime(0, 0, 0, date("m")-11, 01, date("Y")+1)); 
            }else
                $date=date('Y-m-d H:i:s',mktime(0, 0, 0, date("m")+1, 01, date("Y")));
            $update = "UPDATE `mama` SET MAMA_DATETIME = '".$date_1."' WHERE `MAMA_CODE` = '".$var->MAMA_CODE."'";
            $db -> setQuery($update);
            $db -> query();     
        }
    }
    $app = &JFactory::getApplication();
    $app->close(); 
?>